package com.ws.haungjia.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.haungjia.Bean.HoutaiUser;
import com.ws.haungjia.Bean.Huixian;
import com.ws.haungjia.Service.HuserServiec;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/huser")
public class Husercontroller{

    @Autowired
    private HuserServiec huserServiec;

    @RequestMapping("/cha")
    public String tiaozhuan(Model model)
    {
        Huixian huixian = new Huixian();
        PageHelper.startPage(huixian.getYe(), huixian.getGeshu());
        List<HoutaiUser> cha = huserServiec.cha();
        PageInfo pageInfo = new PageInfo(cha);
        model.addAttribute("pageInfo",pageInfo);
        return "WEB-INF/houtai/user/list";
    }

    @RequestMapping("/zengjia")
    public String zengjia(HoutaiUser houtaiUser ,MultipartFile touxinag) throws IOException {
        if (touxinag.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + touxinag.getOriginalFilename().split("\\.")[1];
            houtaiUser.setHtouxiang(fileName);
//            lastIndexo
            touxinag.transferTo(new File(Res.fileDir + fileName));
        }else{
            houtaiUser.setHtouxiang("");
        }
        String s = DigestUtils.md5DigestAsHex(houtaiUser.getHpass().getBytes());
        houtaiUser.setHpass(s);
        int add = huserServiec.zengjia(houtaiUser.getHname(), houtaiUser.getHpass(), new Date(), houtaiUser.getHtouxiang());
        if (add > 0) {
            return "redirect:/huser/cha";
        }else {
            return "qianduan/zhuce";
        }
    }

    @RequestMapping("/shan")
    public String shan(int id){
        int shan = huserServiec.shan(id);
        if (shan > 0) {
            return "redirect:/huser/cha";
        }else {
            return "redirect:/huser/cha";
        }
    }

    @RequestMapping("/huixian")
    public String huixian(int id ,Model model) throws IOException {
        HoutaiUser huixian = huserServiec.huixian(id);
        model.addAttribute("huser",huixian);
        return "WEB-INF/houtai/user/xiugai";
    }

    @RequestMapping("/xiugai")
    public String xiugai(String name, String pass, int id, MultipartFile touxinag) throws IOException {
        HoutaiUser houtaiUser = new HoutaiUser();
        houtaiUser.setHname(name);
        houtaiUser.setHid(id);
        System.out.println(houtaiUser);
        if (touxinag.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + touxinag.getOriginalFilename().split("\\.")[1];
            houtaiUser.setHtouxiang(fileName);
            touxinag.transferTo(new File(Res.fileDir + fileName));
        }else{
            houtaiUser.setHtouxiang("");
        }
        String s = DigestUtils.md5DigestAsHex(pass.getBytes());
        houtaiUser.setHpass(s);
        int xiugai = huserServiec.xiugai(houtaiUser);
        if (xiugai > 0) {
            return "redirect:/huser/cha";
        }else {
            return "WEB-INF/houtai/user/xiugai";
        }
    }

    @RequestMapping("/sousuo")
    public String sousuo(Huixian huiXian , Model model) {
        PageHelper.startPage(huiXian.getYe(), huiXian.getGeshu());
        List<HoutaiUser> a = huserServiec.sousuo(huiXian);
        PageInfo pageInfo = new PageInfo(a);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("huixian",huiXian);
        return "/WEB-INF/houtai/user/list";
    }



    @RequestMapping("chongzhi")
    public String chongzhi(int id,String mima) {
        String s = DigestUtils.md5DigestAsHex(mima.getBytes());
        int chongzhi = huserServiec.chongzhi(id, s);
        if (chongzhi > 0) {
            return "redirect:/huser/cha";
        }else {
            return "WEB-INF/houtai/user/chongzhi";
        }

    }

    @RequestMapping("/addyemian")
    public String addyemian()
    {
        return "WEB-INF/houtai/user/add";
    }
}
